#include<iostream>
#include<vector>
using namespace std;
//将x减到0的最小操作数
class Solution 
{
public:
    int minOperations(vector<int>& nums, int x) 
    {
        int tmp = 0;
        int n = nums.size();
        for (int ch : nums)
            tmp += ch;
        int target = tmp - x;
        if (target < 0)
            return -1;
        int ret = -1;
        for (int left = 0, right = 0, sum = 0; right < n; right++)
        {
            sum += nums[right];
            while (sum > target)
                sum -= nums[left++];
            if (sum == target)
                ret = max(ret, right - left + 1);
        }
        if (ret == -1)
            return ret;
        else
            return n - ret;
    }
};
int main()
{
    Solution s;
    vector<int> arr= { 3,2,20,1,1,3 };
    int x = 10;
    cout << s.minOperations(arr,x)<<endl;
	return 0;
}